/*
 * Copyright (C) 2015 备胎金服
 * 未经授权允许不得进行拷贝和修改
 *   http://www.btjf.com/
 */
package com.zhangman.framework.implementation;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.Environment;
import android.preference.PreferenceManager;
import com.zhangman.framework.FileIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by zhangman on 2016/11/1 11:30.
 * Email: zhangman523@126.com
 */
public class AndroidFileIO implements FileIO {
  Context mContext;
  AssetManager mAssetManager;
  String externalStoragePath;

  public AndroidFileIO(Context context) {
    mContext = context;
    this.mAssetManager = context.getAssets();
    this.externalStoragePath =
        Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
  }

  @Override public InputStream readFile(String file) throws IOException {
    return new FileInputStream(externalStoragePath + file);
  }

  @Override public OutputStream writeFile(String file) throws IOException {
    return new FileOutputStream(externalStoragePath + file);
  }

  @Override public InputStream readAsset(String file) throws IOException {
    return mAssetManager.open(file);
  }

  @Override public SharedPreferences getSharedPreferences() {
    return PreferenceManager.getDefaultSharedPreferences(mContext);
  }
}
